package test2;

import java.util.Scanner;
import java.util.Random;

public class GuessGame {

    public static int getNum()
    {
        int ret = 0;
        //生成1~10的数
        //ret = (int)(Math.random() * 10 + 1);
        //实例化Random类
        Random r = new Random();
        //生成1~10的数
        ret = r.nextInt(10) + 1;
        return ret;
    }
    public static void main(String[] args)
    {
        int ret = getNum();
        int n = 3;
        Scanner sc = new Scanner(System.in);
        System.out.println("猜一个1~10的数");
        while(n != 0) {
            n--;
            int guess = sc.nextInt();
            if (ret == guess) {
                System.out.println("恭喜你，猜对了！");
                break;
            } else if (ret > guess) {
                System.out.println("猜小了!你还有" + n + "次机会");

            } else {
                System.out.println("猜大了!你还有" + n + "次机会");

            }
        }
        System.out.println("这个数是" + ret);
        //关闭资源
        sc.close();
    }
}
